package com.ljj.controller;

import com.ljj.VO.ResultVO;
import com.ljj.VO.SellerApiVO;
import com.ljj.VO.SupportsVO;
import com.ljj.dataobject.ShopInfo;
import com.ljj.exception.SellException;
import com.ljj.form.ShopForm;
import com.ljj.service.impl.ShopInfoServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 卖家信息controller
 * */
@Controller
@RequestMapping("/api/seller")
public class SellerInfoController {

    @Autowired
    private ShopInfoServiceImpl shopInfoService;

    @GetMapping("/list1")
    @ResponseBody
    public ResultVO list1(){
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("成功");

        SupportsVO supportsVO1 = new SupportsVO(0,"在线支付满28减5");
        SupportsVO supportsVO2 = new SupportsVO(1,"在线支付满28减5");
        SupportsVO supportsVO3 = new SupportsVO(2,"在线支付满28减5");
        SupportsVO supportsVO4 = new SupportsVO(3,"在线支付满28减5");
        SupportsVO supportsVO5 = new SupportsVO(4,"在线支付满28减5");
        List<SupportsVO> supportsLsit =  new ArrayList<SupportsVO>();
        supportsLsit.add(supportsVO1);
        supportsLsit.add(supportsVO2);
        supportsLsit.add(supportsVO3);
        supportsLsit.add(supportsVO4);
        supportsLsit.add(supportsVO5);

        List<String> picsList = new ArrayList();
        picsList.add("http://fuss10.elemecdn.com/8/71/c5cf5715740998d5040dda6e66abfjpeg.jpeg?imageView2/1/w/180/h/180");
        picsList.add("http://fuss10.elemecdn.com/b/6c/75bd250e5ba69868f3b1178afbda3jpeg.jpeg?imageView2/1/w/180/h/180");
        picsList.add("http://fuss10.elemecdn.com/f/96/3d608c5811bc2d902fc9ab9a5baa7jpeg.jpeg?imageView2/1/w/180/h/180");
        picsList.add("http://fuss10.elemecdn.com/6/ad/779f8620ff49f701cd4c58f6448b6jpeg.jpeg?imageView2/1/w/180/h/180");

        List<String> infoList = new ArrayList();
        infoList.add("该商家支持发票,请下单写好发票抬头");
        infoList.add("北京市昌平区回龙观西大街龙观置业大厦底商B座102单元1340");


        SellerApiVO sellerApiVO =
                new SellerApiVO("卖卖卖卖",
                        "蜂鸟专送",
                        38,
                        4,
                        4,
                        4,
                        70,
                        5,
                        5,
                        24,
                        90,
                        "卖吃的",
                        supportsLsit,
                        "https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=e3a832add73f8794d3ff4f28ea2069c9/023b5bb5c9ea15ce473e311cb5003af33b87b2d7.jpg",
                        picsList,
                        infoList

                        );
        resultVO.setData(sellerApiVO);
        return resultVO;
    }

    @GetMapping("/list")
    @ResponseBody
    @Cacheable(cacheNames = "sellerInfo",key = "2")
    public ResultVO list(){
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        ShopInfo shopInfo = shopInfoService.findOne(1);

        SupportsVO supportsVO = new SupportsVO(0,shopInfo.getSupports());
        List<SupportsVO> supportsLsit =  new ArrayList<SupportsVO>();
        supportsLsit.add(supportsVO);

        List<String> picsList = new ArrayList();
        picsList.add(shopInfo.getPics());

        List<String> infoList = new ArrayList();
        infoList.add(shopInfo.getInfo());

        SellerApiVO sellerApiVO = new SellerApiVO(shopInfo.getName(),shopInfo.getDescription(),shopInfo.getDeliveryTime(),shopInfo.getScore(),
                shopInfo.getServiceScore(),shopInfo.getFoodScore(),shopInfo.getRankRate(),shopInfo.getMinPrice(),shopInfo.getDeliveryPrice(),shopInfo.getRatingCount(),shopInfo.getSellCount(),
                shopInfo.getBulletin(),supportsLsit,shopInfo.getAvatar(),picsList,infoList);
        resultVO.setData(sellerApiVO);
        return resultVO;
    }

    /**
     * 展示
     */
    @GetMapping("/index")
    public ModelAndView index(Map<String, Object> map) {
        ShopInfo shopInfo = shopInfoService.findOne(1);
        map.put("shopInfo",shopInfo);

        return new ModelAndView("shop/index", map);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @CacheEvict(cacheNames = "sellerInfo",key = "2")
    public ModelAndView save(@Valid ShopForm form,
                             Map<String, Object> map) {
        ShopInfo shopInfo = new ShopInfo();
        try {
            if (form.getShopId() != null) {
                shopInfo = shopInfoService.findOne(form.getShopId());
            }
            BeanUtils.copyProperties(form, shopInfo);
            shopInfoService.save(shopInfo);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/seller/category/index");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/api/seller/index");
        return new ModelAndView("common/success", map);
    }
}
